;;;
;;; SimpleBlink.S
;;; 
        
#define _SFR_ASM_COMPAT 1
#define __SFR_OFFSET 0

#include <avr/io.h>

.global main

;;;
;;; main - initialize port and blink a led
;;; 

main:           ldi     r16,0b00111100          ; all ports are for output
                out     DDRD,r16                ;
loop:           ldi     r30,lo8(pattern)        ; let Z point to the pattern
                ldi     r31,hi8(pattern)        ;
                ldi     r17,10                  ; pattern is 10 bytes
pattern_loop:   ld      r16,Z+                  ;
                out     PORTD,r16               ; Turn the leds on
                ldi     r19,250                 ; delay
                rcall   delay                   ;
                dec     r17                     ; next pattern
                brne    pattern_loop            ;        
                rjmp    loop                    ; loop forever

pattern:
                .byte   0b00111100
                .byte   0b00011000
                .byte   0b00111100
                .byte   0b00011000
                .byte   0b00000000
                .byte   0b00100000
                .byte   0b00010000
                .byte   0b00001000
                .byte   0b00000100
                .byte   0b00000000
        
;;; 
;;; delay - delay for N milliseconds
;;;
;;;   The timing of this function only works on a 16Mhz CPU
;;;
;;;   r19 contains the number of milliseconds
;;;   r21/r22 will be used as work registers
;;; 

delay:
                
                ldi     r22,246           ; 1 N A
delay_2:        ldi     r21,15            ; 1 N A B
delay_1:        nop                       ; 1 N A B
                dec     r21               ; 1 N A B
                brne    delay_1           ; 1 N A B + (2 N A)        
                dec     r22               ; 1 N A
                brne    delay_2           ; 1 N A + (2 N)        
                dec     r19               ; 1 N
                brne    delay             ; 1 N + 2
                ret                       ; 4

#if 0
.global pattern
                .data
                .type   pattern, @object
                .size   pattern, 10
pattern:
                .byte   0b00111100
                .byte   0b00011000
                .byte   0b00111100
                .byte   0b00011000
                .byte   0b00000000
                .byte   0b00100000
                .byte   0b00010000
                .byte   0b00001000
                .byte   0b00000100
                .byte   0b00000000

.global pins
                .type   pins, @object
                .size   pins, 4
pins:
                .byte   2
                .byte   3
                .byte   4
                .byte   5
                .text
        
#endif
        